import json
from dao.model_layersdao import ModelLayersDAO
from dto.trainDTO import TrainDTO
from log import log
from util.model_listutil import ModelListUtil
from dao.traindatadao import TrainDataDao
from dao.evaluationdao import EvaluationDao


class TrainService:
    # train_data: TrainData
    # evaluations: list[Evaluation]
    # model_describe: list  # 将model_list转义成文字
    def get_model_describe(self, model_list: list):
        res = []
        dao = ModelLayersDAO()
        for item in model_list:
            name = dao.findOne(item[0])
            if name == 'ConvBase':
                print(1111111)
                item[1] = item[1].__name__

            res.append([name, *item[1:]])
        return res

    def setDTO(self, train_id: int) -> TrainDTO:
        # m=Model()

        dto = TrainDTO()
        train_data = TrainDataDao().findById(train_id)
        dto.train_data = train_data
        model_list = ModelListUtil().get_model_list_from_db(train_id)
        desc = TrainService().get_model_describe(model_list)
        dto.model_describe = desc
        evaluations = EvaluationDao().getByTrainId(train_id)
        dto.evaluations = list(evaluations)
        return dto
    def showdto(self, dto: TrainDTO):
        print(f'count_epoch={dto.train_data.count_epoch} decay={dto.train_data.decay} learning_rate={dto.train_data.learning_rate} image_height={dto.train_data.image_height} image_width{dto.train_data.image_width} epoch_acc_img_name={dto.train_data.epoch_acc_img_name}')
        print("model_describe")
        for item in dto.model_describe:
            print(item)

if __name__ == "__main__":
    s = TrainService()
    model_list = ModelListUtil().get_model_list_from_db(504)
    l = s.get_model_describe(model_list)
    log.debug(l)
    for i in l:
        print(i)
    # dto = TrainService().setDTO(20)
    # print(dto)
    #
    # evaluations = json.dumps(dto.evaluations, default=lambda o: o.__dict__, ensure_ascii=False)
    # model_describe = json.dumps(dto.model_describe, default=lambda o: o.__dict__, ensure_ascii=False)
    # train_data = json.dumps(dto.train_data, default=lambda o: o.__dict__, ensure_ascii=False)
    # j = json.dumps({
    #     "json_evaluations": evaluations,
    #     "model_describe": model_describe,
    #     "train_data": train_data
    # }, default=lambda o: o.__dict__, indent=5, ensure_ascii=False)
    # print("11111111111111111111111")
    # print(j)
    #
    # json_dao = json.dumps(dto, default=lambda o: o.__dict__, indent=5, ensure_ascii=False)
    # print(json_dao)
    # TrainService().showdto(dto)
    # print(s)
